<?php

namespace app\common\cachemodel\zl6;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl6MeditationPlayRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl6_meditation_play_record';

    protected $cacheModel = 'meditation.play_record';

    /**
     * 写入数据
     * @param array $data
     * @return int
     */
    public function SaveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            $this->listDataAdd($data['uid'], [$data['sid'] => $data['start_time']], $data['mid']);  // 更新 冥想课程章节播放信息
            $this->listDataAdd($data['uid'], [$data['mid'] => $id]);    // 更新 冥想课程播放信息
            return (int)$id;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    /**
     * 更新 播放记录信息
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function UpdateData(int $id, array $data)
    {
        try
        {
            $this->where(['id' => $id])->update($data);
            // 更新 缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->setCache($data);
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * 取 用户播放记录 章节ID
     * @param int $uid
     * @param int $mid
     * @return array
     * @throws \Exception
     */
    public function getMeditionSectionPlayRecordPageDataIds(int $uid, int $mid)
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->setListKeyType($mid)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($uid, $mid)
            {
                try
                {
                    $data = $this->where(['uid' => $uid, 'mid' => $mid])->order("start_time {$sortType}")->column('max(start_time) as start_time', 'sid');
                    $data = array_filter($data);
                    $resData = [];
                    foreach ($data as $id => $lastTime)
                    {
                        $resData[$id] = ['start_time' => $lastTime];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'start_time');

        return $resData['data'];
    }

    /**
     * 取 用户 课程播放记录
     * @param int $uid
     * @param int $page
     * @return array
     * @throws \Exception
     */
    public function getPlayRecordPageDataIds(int $uid, int $page)
    {
        return CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->setPage($page)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($uid)
            {
                try
                {
                    $data = $this->where(['uid' => $uid])->order("id {$sortType}")->group('mid')->column('max(id) as id', 'mid');
                    $data = array_filter($data);
                    $resData = [];
                    foreach ($data as $mid => $id)
                    {
                        $resData[$mid] = ['id' => $id];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id');
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            return $this->where(['id' => ['in', $ids]])->column('uid,mid,sid,duration,start_time,end_time', 'id');
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return [];
        }
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'mid', 'sid', 'duration', 'start_time', 'end_time']);
        }

        return $data;
    }
}